package com.daisuxia.back.controller;

import java.io.BufferedReader;

import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.daisuxia.back.service.IBannerService;
import com.daisuxia.back.utils.DwzResult;
import com.daisuxia.back.utils.PropertiesUtil;
import com.daisuxia.back.utils.SpringUtils;
import com.daisuxia.back.utils.SysCacheUtils;
import com.daisuxia.web.controller.BaseController;
import com.daisuxia.web.pojo.BackConfigParams;
import com.daisuxia.web.pojo.Banner;
import com.daisuxia.web.util.PageConfig;

@Controller
@RequestMapping("banner/")
public class BannerController extends BaseController{

	@Autowired
	private IBannerService bannerService;
	
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> params = getParametersO(request);
		String showType = request.getParameter("showType");
		String channelType = request.getParameter("channelType");
		String columnType = request.getParameter("columnType");
		String sortType = request.getParameter("sortType");
		if (StringUtils.equals(channelType, "全部")) {
			params.remove("channelType");
		}
		if (StringUtils.equals(showType, "全部")) {
			params.remove("showType");
		}
		if (StringUtils.equals(columnType, "全部")) {
			params.remove("columnType");
		}
		if (StringUtils.equals(sortType, "无排序")) {
			params.remove("sortType");
		}
		ModelAndView mav = new ModelAndView("banner/list");
		mav.addObject("params", params);
		PageConfig<Banner> pageConfig = bannerService.findPage(params);
		mav.addObject("pm", pageConfig);
		return mav;
	}
	
	@RequestMapping("saveOrUpdateBannerView")
	public ModelAndView saveOrUpdateView(Integer id, HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> params = getParametersO(request);
		ModelAndView mav = new ModelAndView("banner/saveOrUpdate");
		mav.addObject("params", params);
		if (id == null) {
			return mav;
		}
		Banner banner = bannerService.findById(id);
		mav.addObject("banner", banner);
		return mav;
	}
	
	@RequestMapping(value = "saveOrUpdateBanner", method = RequestMethod.POST)
	public void saveOrUpdate(Integer id, HttpServletRequest request, HttpServletResponse response, Model model, Banner banner, @RequestParam("picture") MultipartFile file) {
		String msg = "操作成功";
		boolean succ = true;
		if (!file.isEmpty()) {// 判断是否有上传文件
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			String path = ((SysCacheUtils) request).getServletContext().getRealPath("/files") + File.separator + "images_banner" + File.separator + format.format(new Date());
			File f = new File(path);
			if (!f.exists()) {
				f.mkdirs();
			}
			long time = System.currentTimeMillis();
			String originalFilename = file.getOriginalFilename();
			String ext = "";
			if (StringUtils.isNotBlank(originalFilename)) {
				ext = originalFilename.substring(originalFilename.lastIndexOf("."));
			}
			path += File.separator + time + ext;
			File newFile = new File(path);
			try {
				file.transferTo(newFile);
				Map<String, String> keys = SysCacheUtils
						.getConfigParams(BackConfigParams.APP_IMG_URL);
				String appurl = keys.get("APP_IMG_URL");
				String url = appurl + "files/images_banner/" + format.format(new Date()) + "/" + time + ext;
//				String url = "http://192.168.1.134:" + request.getServerPort() + "/backcashman/" + "files/images_banner/" + format.format(new Date()) + "/" + time + ext;
				banner.setUrl(url);
			} catch (Exception e) {
				msg = "上传出错";
				succ = false;
				e.printStackTrace();
				return;
			}
		}
		HashMap<String, Object> params = getParametersO(request);
		model.addAttribute("params", params);
		/*String presentWay = request.getParameter("presentWay");
		if (StringUtils.isBlank(presentWay)) {
			msg = "操作有误";
			succ =false;
			SpringUtils.renderDwzResult(response, succ, msg, DwzResult.CALLBACK_CLOSECURRENTDIALOG);
			return;
		}
		if (presentWay.equals("0")) {// 立即发布
			banner.setStartTime("0");
			banner.setEndTime("0");
		}*/
		if (id == null) { // 保存
			try {
				bannerService.save(banner);
			} catch (Exception e) {
				msg = "保存失败";
				succ = false;
				e.printStackTrace();
			}
		} else { // 更新
			Banner old = bannerService.findById(id);
			if (old == null) {
				msg = "数据不存";
				succ = false;
			} else {
				try {
					if (StringUtils.isNotBlank(banner.getShowType())) {
						old.setShowType(banner.getShowType());
					}
					if (StringUtils.isNotBlank(banner.getChannelType())) {
						old.setChannelType(banner.getChannelType());
					}
					if (StringUtils.isNotBlank(banner.getColumnType())) {
						old.setColumnType(banner.getColumnType());
					}
					if (StringUtils.isNotBlank(banner.getTitle())) {
						old.setTitle(banner.getTitle());
					}
					if (StringUtils.isNotBlank(banner.getReurl())) {
						old.setReurl(banner.getReurl());
					}
					if (StringUtils.isNotBlank(banner.getUrl())) {
						old.setUrl(banner.getUrl());
					}
					if (StringUtils.isNotBlank(banner.getSort())) {
						old.setSort(banner.getSort());
					}
					if (StringUtils.isNotBlank(banner.getPresentWay())) {
						old.setPresentWay(banner.getPresentWay());
					}
					if (StringUtils.isNotBlank(banner.getStartTime())) {
						old.setStartTime(banner.getStartTime());
					}
					if (StringUtils.isNotBlank(banner.getEndTime())) {
						old.setEndTime(banner.getEndTime());
					}
					if (StringUtils.isNotBlank(banner.getStatus())) {
						old.setStatus(banner.getStatus());
					}
					bannerService.update(old);
				} catch (Exception e) {
					msg = "更新失败";
					succ = false;
					e.printStackTrace();
				}
			}
		}
		SpringUtils.renderDwzResult(response, succ, msg, DwzResult.CALLBACK_CLOSECURRENTDIALOG);
	}
	
	@RequestMapping("deleteBanner")
	public void delete(Integer id, HttpServletRequest request, HttpServletResponse response) {
		String msg = "删除成功";
		boolean succ = true;	
		if (id == null) {
			msg = "删除失败";
			succ = false;
			SpringUtils.renderDwzResult(response, succ, msg);
			return;
		}
		try {
			// 删除服务器上的图片和数据库中的记录
			Banner ad = bannerService.findById(id);
			if (ad != null) {
				String url = ad.getUrl();
				if (StringUtils.isNotBlank(url) && url.contains("/files")) {
					url = url.substring(url.indexOf("/files"));
					String[] strs = url.split("/");
					String path = ((SysCacheUtils) request).getServletContext().getRealPath("/");
					for (String str : strs) {
						if (StringUtils.isNotBlank(str)) {
							path += str + File.separator;
						}
					}
					File file = new File(path);
					if (file.exists()) {
						file.delete();
					}
				}
				bannerService.delete(id);
			}
		} catch (Exception e) {
			msg = "删除失败";
			succ = false;
			e.printStackTrace();
		}
		SpringUtils.renderDwzResult(response, succ, msg, DwzResult.CALLBACK_CLOSECURRENTDIALOG);
	}
	
	
	
	@RequestMapping("bannerUpdateCache")
	public void bannerUpdateCache(HttpServletRequest request, HttpServletResponse response,Model model) {

		boolean succ = false;
		Map<String, String> params =this.getParameters(request);
		model.addAttribute("params", params);

		String localhostBack = request.getServerName() + ":" + request.getServerPort();
		String msg = "刷新缓存失败";
		Set<String> failUrl = new HashSet<String>();
		try {
			 
			int del = 0;
			del = bannerService.deleteIndexCache();
			String urlString = PropertiesUtil.get("BANNER_URLS");
//			if(del==1){
				bannerUpdateOther(failUrl, urlString, localhostBack);
				if ((failUrl == null || failUrl.size() == 0)) {
					succ = true;
					msg="刷新缓存成功";
				}
				succ = true;
//			}
		} catch (Exception e) {
			e.printStackTrace();
			succ = false;
			msg = "刷新缓存失败";
		} finally {

		}

		if (failUrl != null && failUrl.size() > 0) {
			succ = false;
			msg = "刷新失败，失败的服务器列表：" + failUrl.toString();
		}

		 SpringUtils.renderDwzResult(response, succ,msg, DwzResult.CALLBACK_CLOSECURRENT,params.get("parentId")
					.toString());

	}
	
	public void bannerUpdateOther(Set<String> failUrl, String urlString, String currentIp) {

		if (StringUtils.isNotBlank(urlString)) {
			String method = PropertiesUtil.get("UPDATE_METHOD");
			String[] urls = urlString.split(",");
			for (String string : urls) {
				try {
					if (string.indexOf(currentIp) > 0) {// 不刷新本机
						continue;
					}
					URL url = new URL(string + method);
					// 将url 以 open方法返回的urlConnection
					// 连接强转为HttpURLConnection连接 (标识一个url所引用的远程对象连接)
					HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中

					// 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)
					connection.setDoOutput(true);

					// 设置连接输入流为true
					connection.setDoInput(true);

					// 设置请求方式为post
					connection.setRequestMethod("POST");

					// post请求缓存设为false
					connection.setUseCaches(false);

					// 设置该HttpURLConnection实例是否自动执行重定向
					connection.setInstanceFollowRedirects(true);
					connection.setConnectTimeout(3000);
					connection.setReadTimeout(3000);
					// 设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
					// application/x-javascript text/xml->xml数据
					// application/x-javascript->json对象
					// application/x-www-form-urlencoded->表单数据
					connection.setRequestProperty("Content-Type", "application/x-javascript");

					// 建立连接
					// (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
					connection.connect();

					// 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
					DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
					// String parm = "storeId="
					// + URLEncoder.encode("32", "utf-8"); //
					// URLEncoder.encode()方法
					// 为字符串进行编码

					// 将参数输出到连接
					// dataout.writeBytes(parm);

					// 输出完成后刷新并关闭流
					dataout.flush();
					dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)
					int reponseCode = connection.getResponseCode();
					if (200 != reponseCode) {
						failUrl.add(string);
					}

					// 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader)
					BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
					String result = bf.readLine();
					if (result != null && !"".equals(result)) {
						JSONObject json = JSONObject.fromObject(result);
						DwzResult dwzResult = (DwzResult) JSONObject.toBean(json, DwzResult.class);
						if (!(dwzResult != null && "200".equals(dwzResult.getStatusCode()))) {
							failUrl.add(string);
						}
					} else {
						failUrl.add(string);
					}
					bf.close(); // 重要且易忽略步骤 (关闭流,切记!)
					connection.disconnect(); // 销毁连接

				} catch (Exception e) {
					e.printStackTrace();
					failUrl.add(string);
				}
			}
		}
	}
	
	
}
